home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / aijournl / 1987_01 / percep.jan < prev   
Text File  |  1986-12-21  |  13KB  |  338 lines

  1.  
  2.  
  3.                          Perceptrons & Neural Nets
  4.               (Two slightly different versions of the program)
  5.                           January 1987 AI EXPERT
  6.                               by Peter Reece
  7.  
  8.  
  9. Listing 1
  10.  
  11. 5 ' PERCEPTRON VISION SYSTEM SIMULATION, Peter Reece 1986
  12. 10 '
  13. 11 '
  14. 12 DEFINT A-X
  15. 13 ' IMAGE()    = the sensory grid array
  16. 14 ' NEURALNET  = the associative net - neural interconnections
  17. 15 ' SIZE**2    = number of cells in the sensory grid
  18. 16 ' SCAN       = number of cells required to construct an 8-bit address
  19. 17 '              into the array NEURALNET()
  20. 18 ' LOOPSCAN   = the number of iterations for scanning the sensory
  21. 19 '              grid - i.e. we look at scan cells at random 
  22. 20 '              loopscan times
  23. 21 SIZE=16:SCAN=8:LOOPSCAN=SIZE*SIZE/SCAN
  24. 22 DIM IMAGE(SIZE,SIZE),NEURALNET(LOOPSCAN*SIZE*SIZE,1):CLS
  25. 30 PRINT"  This program demonstrates how a very simple"
  26. 40 PRINT"pecpeptron is capable of analysing visual information."
  27. 45 PRINT:PRINT:PRINT
  28. 50 PRINT"  Proceed as follows: "
  29. 51 PRINT:PRINT
  30. 60 PRINT" 1) Draw an object and decide if that object is a member of"
  31. 70 PRINT"    a ojbect class one or two. Eg. A cup, saucer, and "
  32. 80 PRINT"    plate might be class 1, a crayon class 2."
  33. 81 PRINT" 2) Train the perceptron to recognize objects"
  34. 82 PRINT"    of a particular class by drawing various objects"
  35. 83 PRINT"    from both classes."
  36. 84 PRINT" 3) Present various objects to the perceptron, (some"
  37. 85 PRINT"    old objects may be used, as well as those that it"
  38. 86 PRINT"    has never seen before), and see how successfully it"
  39. 87 PRINT"    classifies new ojects as beloging to the correct class."
  40. 88 PRINT:PRINT
  41. 90 Q$="Press [enter] to begin a training session.":GOSUB 3000:CLS
  42. 98 '
  43. 99 '
  44. 100 '
  45. 110 '********** Reach here to begin a training session. *********
  46. 120 CLS:FOR I=1 TO SIZE:FOR J=1 TO SIZE:IMAGE(I,J)=0:NEXT:NEXT
  47. 130 LOCATE 10,50:PRINT"===    TRAINING SESSION    ===="
  48. 135 LOCATE 11,50:INPUT"Draw class 1 or 2";CLASS
  49. 136 IF CLASS=1 THEN CLASS=0 ELSE CLASS=1
  50. 140 '
  51. 145 GOSUB 1000
  52. 150 FOR I=1 TO LOOPSCAN:GOSUB 2000
  53. 160 NEURALNET(INDEX,CLASS)=1
  54. 210 NEXT
  55. 220 LOCATE 2,5:Q$="Want to conduct another training session"
  56. 230 GOSUB 3000:IF Q$="Y" THEN 110
  57. 231 '
  58. 232 '
  59. 233 '
  60. 400 ' *********** Here to classify an object ************
  61. 410 FOR I=1 TO SIZE:FOR J=1 TO SIZE:IMAGE(I,J)=0:NEXT:NEXT
  62. 420 CLS:LOCATE 10,50:PRINT"=== CLASSIFICATION SESSION ===="
  63. 422 '
  64. 450 GOSUB 1000:MEMBER=0:NONMEMBER=0
  65. 500 FOR I=1 TO LOOPSCAN:GOSUB 2000
  66. 540 IF NEURALNET(INDEX,0)=1 THEN MEMBER=MEMBER+1
  67. 550 IF NEURALNET(INDEX,1)=1 THEN NONMEMBER=NONMEMBER+1
  68. 560 NEXT
  69. 571 LOCATE 23,2:PRINT SPC(78)
  70. 573 LOCATE 12,50: PRINT"Ratio is ";MEMBER;"/";NONMEMBER
  71. 574 LOCATE 13,50: PRINT " favoring class ";
  72. 576 IF MEMBER<NONMEMBER THEN 580
  73. 577 PRINT"One":GOTO 590
  74. 580 PRINT"Two"
  75. 590 LOCATE 14,50:Q$="Classify another object":GOSUB 3000
  76. 600 IF Q$="Y" THEN 400
  77. 601'
  78. 610 CLS:?:?:?:?:Q$="Want to see NEURALNET":GOSUB 3000
  79. 620 IF Q$="N" THEN  670
  80. 630 K=0:KK=0:KZ=0:FOR I=1 TO LOOPSCAN*SIZE*SIZE
  81. 640 FOR J=0 TO 1
  82. 650 A=NEURALNET(I,J):IF A=1 THEN ?"*"; ELSE ?".";
  83. 660 K=K+1:IF K>SIZE THEN K=0:KZ=KZ+1:?"  ";
  84. 661 IF KZ>3 THEN KZ=0:?:KK=KK+1
  85. 662 IF KK>SIZE THEN KK=0:?
  86. 665 NEXT:NEXT
  87. 670 ?"Emptying Neural Network...":FOR I=1 TO LOOPSIZE*SIZE*SIZE
  88. 680 FOR J=0 TO 1
  89. 690 NEURALNET(I,J)=0:NEXT:NEXT:GOTO 100
  90. 700 :CLS:PRINT"Bye!":STOP
  91. 998 '
  92. 999 '
  93. 1000 ' *********** Interactive Object drawing ***********
  94. 1005 RR=5:CC=20:ROW=1:CLM=1
  95. 1006 LOCATE 23,2
  96. 1010 PRINT"[D],[U],[L],[R] to move. [.] to plot, [ ] to erase, [S] to stop."
  97. 1061 FOR I=1 TO SIZE+1
  98. 1062 LOCATE RR+I,CC:PRINT "|";:LOCATE RR+I,CC+17:PRINT "|";
  99. 1063 LOCATE RR,CC+I:PRINT "-";:LOCATE RR+17,CC+I:PRINT"-";
  100. 1064 NEXT
  101. 1070 LOCATE ROW+RR,CLM+CC
  102. 1080 A$=INKEY$:IF A$="" THEN 1080
  103. 1090 IF A$="U" THEN ROW=ROW-1
  104. 1100 IF A$="D" THEN ROW=ROW+1
  105. 1110 IF A$="R" THEN CLM=CLM-1
  106. 1120 IF A$="L" THEN CLM=CLM+1
  107. 1130 IF CLM > SIZE THEN CLM=SIZE
  108. 1140 IF CLM < 1 THEN CLM=1
  109. 1160 IF ROW < 1 THEN ROW=1
  110. 1170 IF ROW > SIZE THEN ROW=SIZE
  111. 1171 LOCATE 5,5:PRINT "ROW=";ROW;" CLM=";CLM;
  112. 1190 LOCATE ROW+RR,CLM+CC
  113. 1191 IF A$="." THEN PRINT"*":LOCATE ROW+RR,CLM+CC:IMAGE(ROW,CLM)=1
  114. 1194 IF A$=" " THEN PRINT" ":LOCATE ROW+RR,CLM+CC:IMAGE(ROW,CLM)=0
  115. 1196 IF A$="S" THEN LOCATE 10,1:PRINT"Object completed":GOTO 1210
  116. 1205 GOTO 1080
  117. 1210 PRINT "ONE MOMENT...":RETURN
  118. 1999'
  119. 2000 ' Calculate an SCAN digit address into NEURALNET()
  120. 2001 ' by scanning any 8 cells of IMAGE() at random
  121. 2002 ' If a cell has an active pixel, it is considered on,
  122. 2003 ' else it is considered off. Hence a SCAN digit binary address.
  123. 2005 INDEX=SIZE*SIZE*(I-1)
  124. 2010 FOR J=1 TO SCAN
  125. 2020 FIRST=INT(RND*SIZE+1):SECOND=INT(RND*SIZE+1)
  126. 2040 INDEX=INDEX+IMAGE(FIRST,SECOND)*2^J
  127. 2050 NEXT:RETURN
  128. 2999 '
  129. 3000 PRINT Q$;:INPUT " ";Q$
  130. 3010 Q$=LEFT$(Q$,1):RETURN
  131.  
  132.  
  133. Listing 2
  134.  
  135. 10' Simulation of a Simple Neural Net 
  136. 20 ' IMAGE      = the sensory grid array
  137. 30 ' NEURALNET  = the associative net - neural interconnections
  138. 40 ' SIZE^2     = number of cells in the sensory grid
  139. 50 ' SCAN       = number of cells required to construct an 8-bit address
  140. 60 '              into the array NEURALNET()
  141. 70 ' LSCAN      = the number of iterations for scanning the sensory
  142. 80 '              grid - i.e. we look at scan cells at random 
  143. 90 '              loopscan times
  144. 100 DEFINT A-Z:SIZE=16:SCAN=8:LSCAN=(SIZE^2)/SCAN
  145. 120 DIM IMAGE(SIZE,SIZE),NEURALNET(LSCAN*(SIZE^2),2)
  146. 130 GOSUB 6000:'                                   Intro message
  147. 140 '
  148. 150 '************ Training session. ************
  149. 155 RANDOMIZE 5:'                                  Init random
  150. 160 CLS: LOCATE 10,50:PRINT"===    TRAINING SESSION    ===="
  151. 161 LOCATE 12,50:'                                 Put up a prompt
  152. 162 Q$="Automatic training"
  153. 163 GOSUB 3000:'                                   Select Training
  154. 164 IF Q$<>"Y" THEN 170:'                          Manual Training
  155. 165 GOSUB 4000:GOTO 400:'                          Automatic Training
  156. 166 '
  157. 170 LOCATE 11,50
  158. 175 INPUT"Draw class 1 or 2";CLASS:'               Select a class
  159. 180 IF CLASS>2 THEN CLASS=2:'                      for this object
  160. 190 IF CLASS<2 THEN CLASS=1:'                      within range
  161. 200 GOSUB 1000:'                                   Draw an object 
  162. 210 FOR I=1 TO LSCAN:'                             Calculate 
  163. 220   GOSUB 2000:'                                 indicies into
  164. 230   NEURALNET(INDEX,CLASS)=1:'                   neuralnet
  165. 240 NEXT:'                                         for this class
  166. 250 LOCATE 2,5
  167. 260 Q$="Want to conduct more training":'           loop through more
  168. 270 GOSUB 3000:IF Q$="Y" THEN 160:'                training 
  169. 271 '
  170. 272 '
  171. 273 '
  172. 400 ' *********** Classification Session ************
  173. 420 CLS:LOCATE 10,50:PRINT"=== CLASSIFICATION SESSION ===="
  174. 430 '
  175. 431 RANDOMIZE 5:'                                  Init random
  176. 440 GOSUB 1000:'                                   Draw an object
  177. 450 MEMBER=0:NONMEMBER=0:'                         Init member count
  178. 500 FOR I=1 TO LSCAN:'                             Calculate 
  179. 510   GOSUB 2000:'                                 indicies 
  180. 540   IF NEURALNET(INDEX,1)=1 THEN MEMBER=MEMBER+1
  181. 550   IF NEURALNET(INDEX,2)=1 THEN NONMEMBER=NONMEMBER+1
  182. 551   IF NEURALNET(INDEX,1)=0 AND NEURALNET(INDEX,2)=0 THEN 553
  183. 552   GOTO 560
  184. 553   I=I-1:'                                       Null class found
  185. 560 NEXT
  186. 571 LOCATE 23,2:PRINT SPC(78)
  187. 573 LOCATE 12,50: PRINT"Ratio is ";MEMBER;"/";NONMEMBER
  188. 574 LOCATE 13,50: PRINT " favouring class ";
  189. 576 IF MEMBER<NONMEMBER THEN 580
  190. 577 PRINT"Two.";:GOTO 588
  191. 580 PRINT"One.";
  192. 588 IF ABS(MEMBER-NONMEMBER)>1 THEN 590
  193. 589 LOCATE 9,50:?" * Ratios is close. *"
  194. 590 LOCATE 14,50:Q$="Classify another object":GOSUB 3000
  195. 600 IF Q$="Y" THEN 400
  196. 601'
  197. 610 CLS:?:?:?:?:Q$="Want to see NEURALNET":GOSUB 3000
  198. 620 IF Q$="Y" THEN GOSUB 7000
  199. 670 ?"Emptying Neural Network..."
  200. 671 FOR I=1 TO LSCAN*(SIZE^2)
  201. 680   FOR J=1 TO 2
  202. 690   NEURALNET(I,J)=0
  203. 691   NEXT
  204. 692 NEXT:GOTO 150
  205. 998 '
  206. 999 '
  207. 1000 ' *********** Interactive Object drawing ***********
  208. 1002 FOR I=1 TO SIZE:FOR J=1 TO SIZE:IMAGE(I,J)=0:NEXT:NEXT
  209. 1005 RR=5:CC=20:ROW=1:CLM=1
  210. 1006 LOCATE 23,2
  211. 1010 PRINT"[D],[U],[L],[R] to move. [.] to plot, [ ] to erase, [S] to stop."
  212. 1061 FOR I=1 TO SIZE+1
  213. 1062   LOCATE RR+I,CC:PRINT "|";:LOCATE RR+I,CC+17:PRINT "|";
  214. 1063   LOCATE RR,CC+I:PRINT "-";:LOCATE RR+17,CC+I:PRINT"-";
  215. 1064 NEXT
  216. 1070 LOCATE ROW+RR,CLM+CC
  217. 1080 A$=INKEY$:IF A$="" THEN 1080
  218. 1090 IF A$="U" THEN ROW=ROW-1
  219. 1100 IF A$="D" THEN ROW=ROW+1
  220. 1110 IF A$="R" THEN CLM=CLM-1
  221. 1120 IF A$="L" THEN CLM=CLM+1
  222. 1130 IF CLM > SIZE THEN CLM=SIZE
  223. 1140 IF CLM < 1 THEN CLM=1
  224. 1160 IF ROW < 1 THEN ROW=1
  225. 1170 IF ROW > SIZE THEN ROW=SIZE
  226. 1171 LOCATE 5,5:PRINT "ROW=";ROW;" CLM=";CLM;
  227. 1190 LOCATE ROW+RR,CLM+CC
  228. 1191 IF A$="." THEN PRINT CHR$(219):LOCATE ROW+RR,CLM+CC:IMAGE(CLM,ROW)=1
  229. 1194 IF A$=" " THEN PRINT" ":LOCATE ROW+RR,CLM+CC:IMAGE(CLM,ROW)=0
  230. 1196 IF A$="S" THEN LOCATE 10,1:PRINT"Object completed":GOTO 1210
  231. 1205 GOTO 1080
  232. 1210 PRINT "ONE MOMENT...":RETURN
  233. 1998'
  234. 1999'
  235. 2000 ' Calculate a SCAN digit address into NEURALNET()
  236. 2001 ' by scanning any SCAN cells of IMAGE() at random
  237. 2002 ' If a cell has an active pixel, it is considered on,
  238. 2003 ' else it is considered off. Hence a SCAN digit binary address.
  239. 2004 ' Resultant index is in the range 0 and up in size^2 
  240. 2005 ' blocks. The address within a block is determined by 
  241. 2006 ' the image(a,b) as a power of 2 (line 2040).
  242. 2009 INDEX=(SIZE^2)*(I-1)
  243. 2010 FOR J=0 TO SCAN-1
  244. 2020   FIRST=INT(RND*SIZE)+1:SECOND=INT(RND*SIZE)+1
  245. 2040   INDEX=INDEX+IMAGE(FIRST,SECOND)*2^J
  246. 2050 NEXT:RETURN
  247. 2999 '
  248. 3000' Issue a prompt using q$, and return q$=Y/N
  249. 3001 PRINT Q$;:INPUT Q$
  250. 3010 Q$=LEFT$(Q$,1):
  251. 3050 RETURN
  252. 3099'
  253. 4000' Train the neural net on vertical vs. horizontal lines
  254. 4001 ?"Note: It takes a while to scan each object, but more "
  255. 4002 ?"      ojects mean more accurate classification."
  256. 4003 CLASS=1:RANDOMIZE 5
  257. 4004 INPUT"How many objects of Class One ";KNT
  258. 4010     FOR KLOOP=1 TO KNT:CLS: LOCATE 10,30:?KLOOP;" of ";KNT
  259. 4011     FOR I=1 TO SIZE+1
  260. 4012       LOCATE I,SIZE:?"|";:LOCATE SIZE,I:? "-";
  261. 4013     NEXT
  262. 4014     ?"Object Class One";
  263. 4015 '     Create one horizontal line of length k
  264. 4019       FOR I=1 TO SIZE:FOR J=1 TO SIZE:IMAGE(I,J)=0:NEXT:NEXT
  265. 4020       KLEN=INT(RND*SIZE+1):IF KLEN<2 THEN 4020
  266. 4021       MPOS=INT(RND*SIZE)+1:NPOS=INT(RND*SIZE)+1
  267. 4022       IF NPOS+KLEN>SIZE THEN 4020
  268. 4023       IF NPOS>=KLEN THEN 4020
  269. 4025       FOR A=NPOS TO KLEN
  270. 4026          IMAGE(A,MPOS)=1:LOCATE MPOS,A:?CHR$(223);
  271. 4027       NEXT
  272. 4029       'Now place this image into nerualnet
  273. 4030       LOCATE 11,30:?"Scanning object"
  274. 4032       LOCATE 12,30:?"Len=";KLEN;" Start=";NPOS;",";MPOS;
  275. 4090       FOR I=1 TO LSCAN:GOSUB 2000
  276. 4091           NEURALNET(INDEX,CLASS)=1
  277. 4092       NEXT
  278. 4094     NEXT:CLS
  279. 4100 INPUT"How many objects of Class Two ";KNT
  280. 4105 CLASS=2:RANDOMIZE 5
  281. 4110      FOR KLOOP=1 TO KNT:CLS:LOCATE 10,30:?KLOOP;" of ";KNT
  282. 4111     FOR I=1 TO SIZE+1
  283. 4112       LOCATE I,SIZE:?"|";:LOCATE SIZE,I:? "-";
  284. 4113     NEXT
  285. 4114     ?"Object Class Two";
  286. 4120       FOR I=1 TO SIZE:FOR J=1 TO SIZE:IMAGE(I,J)=0:NEXT:NEXT
  287. 4130       KLEN=INT(RND*SIZE+1):IF KLEN<2 THEN 4130
  288. 4135       MPOS=INT(RND*SIZE)+1:NPOS=INT(RND*SIZE)+1
  289. 4140       IF NPOS+KLEN>SIZE THEN 4130
  290. 4141       IF NPOS>=KLEN THEN 4130
  291. 4145       FOR A=NPOS TO KLEN
  292. 4150          IMAGE(MPOS,A)=1:LOCATE A,MPOS:?CHR$(219);
  293. 4153       NEXT
  294. 4154       'Now place this image into nerualnet
  295. 4155       LOCATE 11,30:?"Scanning object"
  296. 4156       LOCATE 12,30:?"Len=";KLEN;" Start=";NPOS;",";MPOS;
  297. 4160       FOR I=1 TO LSCAN:GOSUB 2000
  298. 4170           NEURALNET(INDEX,CLASS)=1
  299. 4180       NEXT
  300. 4190     NEXT:CLS
  301. 4200 RETURN
  302. 4998'
  303. 4999'
  304. 6000' Put up an intro message
  305. 6010 CLS:PRINT"  This program demonstrates how a very simple"
  306. 6040 PRINT"pecpeptron is capable of analysing visual information."
  307. 6045 PRINT:PRINT:PRINT
  308. 6050 PRINT"  Proceed as follows: "
  309. 6051 PRINT:PRINT
  310. 6060 PRINT" 1) Draw an object and decide if that object is a member of"
  311. 6070 PRINT"    a ojbect class one or two. Try vertical versus"
  312. 6080 PRINT"    horizontal lines to start."
  313. 6081 PRINT" 2) Train the neural net to recognize objects"
  314. 6082 PRINT"    of a particular class by drawing various objects"
  315. 6083 PRINT"    from both classes. (This may be done automatically)."
  316. 6084 PRINT" 3) Present various objects to the net, (some"
  317. 6085 PRINT"    old objects may be used, as well as those that it"
  318. 6086 PRINT"    has never seen before), and see how successfully it"
  319. 6087 PRINT"    classifies new ojects as belonging to the correct class."
  320. 6088 PRINT"    This simple simulation will make mistakes, but should"
  321. 6089 PRINT"    perform better or even much better than at random."
  322. 6090 PRINT:PRINT
  323. 6091 Q$="Ready.":GOSUB 3000:CLS
  324. 6100 RETURN
  325. 6999'
  326. 7000' Display the contents of the neural network
  327. 7030 K=0:KK=0:KZ=0
  328. 7031 FOR I=1 TO LSCAN*SIZE^2
  329. 7040   FOR J=1 TO 2
  330. 7050     A=NEURALNET(I,J):IF A=1 THEN ?"*"; ELSE ?".";
  331. 7060     K=K+1:IF K>SIZE THEN K=0:KZ=KZ+1:?"  ";
  332. 7061     IF KZ>3 THEN KZ=0:?:KK=KK+1
  333. 7062     IF KK>SIZE THEN KK=0:?
  334. 7065   NEXT
  335. 7066 NEXT
  336. 7070 RETURN
  337.  
  338.